clear all
set mo off
*set your own directory: cd ""

*****************
****Table 2******
*****************

use data_main_analysis, clear

* baseline
reg ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year ///
	stem_f, cluster(provin) 

* contemporary 
reg ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year gdp_pc unemp_rate catholic ///
	social_capital n_household stem_f, cluster(provin) 

* historical and geographical 
reg ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year gdp_pc unemp_rate catholic ///
	social_capital n_household ///
	pop_dens1787 pop_dens1860 pop_dens urban1787 urban1860 ///
	ruggedness temp rainfall temp_range frost stem_f, cluster(provin)

 
*****************
****Table 3******
*****************

* Panel A: Physical and sexual violence
reg phy_sex_ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year ///
	stem_f, cluster(provin) 

reg phy_sex_ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year gdp_pc unemp_rate catholic ///
	social_capital n_household stem_f, cluster(provin) 

reg phy_sex_ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year gdp_pc unemp_rate catholic ///
	social_capital n_household ///
	pop_dens1787 pop_dens1860 pop_dens urban1787 urban1860 ///
	ruggedness temp rainfall temp_range frost stem_f, cluster(provin)

* Panel B: Psychological, economic, spiritual, and structural violence
reg str_eco_spi_psy_ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year ///
	stem_f, cluster(provin) 

reg str_eco_spi_psy_ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year gdp_pc unemp_rate catholic ///
	social_capital n_household stem_f, cluster(provin) 

reg str_eco_spi_psy_ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year gdp_pc unemp_rate catholic ///
	social_capital n_household ///
	pop_dens1787 pop_dens1860 pop_dens urban1787 urban1860 ///
	ruggedness temp rainfall temp_range frost stem_f, cluster(provin)

****************************
****Table A7 Appendix ******
****************************

* Continuous measure of IPV
poisson sum_ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year ///
   stem_f, cluster (provin) 

poisson sum_ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year gdp_pc unemp_rate catholic ///
	social_capital n_household stem_f, cluster(provin) 

poisson sum_ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year gdp_pc unemp_rate catholic ///
	social_capital n_household ///
	pop_dens1787 pop_dens1860 pop_dens urban1787 urban1860 ///
	ruggedness temp rainfall temp_range frost stem_f, cluster(provin)

	
*****************
****Table 4******
*****************

* baseline
ivreg2 ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year ///
	(stem_f=polit_dec), cluster(provin) first

* contemporary 
ivreg2 ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year gdp_pc unemp_rate catholic ///
	social_capital n_household (stem_f=polit_dec), cluster(provin) first

* historical and geographical 
ivreg2 ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year gdp_pc unemp_rate catholic ///
	social_capital n_household ///
	pop_dens1787 pop_dens1860 pop_dens urban1787 urban1860 ///
	ruggedness temp rainfall temp_range frost (stem_f=polit_dec), cluster(provin) first


****************************
****Table A8 Appendix ******
****************************

* Panel A: First-Stage 2SLS Results at the Province Level
preserve
collapse stem_f polit_dec gdp_pc unemp_rate social_capital /// 
	pop_dens1787 pop_dens1860 pop_dens urban1787 urban1860 ///
	ruggedness temp rainfall temp_range frost, by(provin)
reg stem_f polit_dec, r
reg stem_f polit_dec gdp_pc unemp_rate social_capital, r
reg stem_f polit_dec gdp_pc unemp_rate social_capital /// 
	pop_dens1787 pop_dens1860 pop_dens urban1787 urban1860 ///
	ruggedness temp rainfall temp_range frost, r
restore

* Panel B and C: IV results with Binary Stem Family
gen stem_f_b=0
replace stem_f_b=1 if stem_f>1
	
ivreg2 ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year ///
	(stem_f_b=polit_dec), cluster(provin) first

ivreg2 ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year gdp_pc unemp_rate catholic ///
	social_capital n_household (stem_f_b=polit_dec), cluster(provin) first
	
ivreg2 ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year gdp_pc unemp_rate catholic ///
	social_capital n_household ///
	pop_dens1787 pop_dens1860 pop_dens urban1787 urban1860 ///
	ruggedness temp rainfall temp_range frost (stem_f_b=polit_dec), cluster(provin) first


****************************
****Table A9 Appendix ******
****************************

ivreg2 ipv i.age children i.civil_status i.city i.year ///
	(stem_f=polit_dec), cluster(provin) first

ivreg2 ipv i.age children i.civil_status i.city i.year  ///
	ruggedness temp rainfall temp_range frost ///
	(stem_f=polit_dec), cluster(provin) first	


****************************
****Table A10 Appendix ******
****************************

reg ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year ///
	polit_dec, cluster(provin) 
 
reg ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year gdp_pc unemp_rate catholic ///
	social_capital n_household polit_dec, cluster(provin) 

reg ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year gdp_pc unemp_rate catholic ///
	social_capital n_household ///
	pop_dens1787 pop_dens1860 pop_dens urban1787 urban1860 ///
	ruggedness temp rainfall temp_range frost polit_dec, cluster(provin)
	

*****************************
****Table A11 Appendix ******
*****************************
gen north=0
replace north=1 if autono==12 | autono==3 | autono==6 | autono==16 | autono==15 ///
| autono==2 | autono==17 | autono==9 | autono==8 | autono==13 | provin==12 | provin==19
gen south=(north==0)

gen east=0
replace east=1 if autono==2 | autono==4 | autono==9 | autono==10 | autono==14 |autono==15 ///
| autono==16 | autono==17 | autono==7 | provin==4 | provin==18 | provin==23 | provin==9 ///
| autono==13 
gen west=(east==0)

gen east_north=east*north
gen west_north=west*north
gen east_south=east*south
gen west_south=west*south

ivreg2 ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city ///
 i.year gdp_pc unemp_rate catholic social_capital n_household ///
	north ///
	(stem_f=polit_dec), cluster(provin) first

ivreg2 ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city ///
 i.year gdp_pc unemp_rate catholic social_capital n_household ///
	east ///
	(stem_f=polit_dec), cluster(provin) first

ivreg2 ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city ///
 i.year gdp_pc unemp_rate catholic social_capital n_household ///
	east_north west_north east_south ///
	(stem_f=polit_dec), cluster(provin) first

*****************************
****Table A12 Appendix ******
*****************************

plausexog uci ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city ///
    i.year gdp_pc unemp_rate catholic social_capital n_household ///
    pop_dens1787 pop_dens1860 pop_dens urban1787 urban1860 ///
    ruggedness temp rainfall temp_range frost (stem_f=polit_dec), ///
	 gmin(-0.0025) gmax(0.0025) grid(2) level(.9)  vce(cluster provin) 	

plausexog uci ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city ///
    i.year gdp_pc unemp_rate catholic social_capital n_household ///
    pop_dens1787 pop_dens1860 pop_dens urban1787 urban1860 ///
    ruggedness temp rainfall temp_range frost (stem_f=polit_dec), ///
	 gmin(-0.004) gmax(0.004) grid(2) level(.9)  vce(cluster provin) 	 

plausexog uci ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city ///
    i.year gdp_pc unemp_rate catholic social_capital n_household ///
    pop_dens1787 pop_dens1860 pop_dens urban1787 urban1860 ///
    ruggedness temp rainfall temp_range frost (stem_f=polit_dec), ///
	 gmin(-0.0055) gmax(0.0055) grid(2) level(.9)  vce(cluster provin) 	 
	 
plausexog uci ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city ///
    i.year gdp_pc unemp_rate catholic social_capital n_household ///
    pop_dens1787 pop_dens1860 pop_dens urban1787 urban1860 ///
    ruggedness temp rainfall temp_range frost (stem_f=polit_dec), ///
	 gmin(-0.007) gmax(0.007) grid(2) level(.9)  vce(cluster provin) 	
	 
plausexog uci ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city ///
    i.year gdp_pc unemp_rate catholic social_capital n_household ///
    pop_dens1787 pop_dens1860 pop_dens urban1787 urban1860 ///
    ruggedness temp rainfall temp_range frost (stem_f=polit_dec), ///
	 gmin(-0.0085) gmax(0.0085) grid(2) level(.9)  vce(cluster provin)  

	 
****************************
****Table A4 Appendix ******
****************************	 

gen works=1 if occup_status==1
replace works=0 if occup_status==2 | occup_status==3

gen unemployed=1 if occup_status==2
replace unemployed=0 if occup_status==1 | occup_status==3

reg ipv i.age i.educ i.educ_par2 works unemployed children refe i.civil_status i.city ///
    i.year gdp_pc unemp_rate catholic social_capital n_household ///
    pop_dens1787 pop_dens1860 pop_dens urban1787 urban1860 ///
    ruggedness temp rainfall temp_range frost stem_f, cluster(provin)

gen ytilde=ipv-_b[works]
reg ytilde i.age i.city i.year pop_dens1787 pop_dens1860 pop_dens urban1787 urban1860  ///
	ruggedness temp rainfall temp_range frost stem_f,  vce(bootstrap, reps(1000) seed(12345) cluster(provin))
reg  ytilde i.age i.city i.year pop_dens1787 pop_dens urban1787  ///
	ruggedness temp rainfall temp_range frost stem_f,  vce(bootstrap, reps(1000) seed(12345) cluster(provin))


****************************
****Figure A2 Appendix ******
****************************	

reg ipv i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year, cluster(provin)
predict residY,resid

reg stem_f i.age i.educ i.educ_par2 i.occup_status children refe i.civil_status i.city i.year, cluster(provin)
predict residX,resid	
reg residY residX

bys provin: egen mean_residY=mean(residY)
su mean_residY,d
bys provin: egen mean_residX=mean(residX)
su mean_residX,d
bys provin: gen one=1 if _n==1
label var mean_residY "Mean resid. IPV"
label var mean_residX "Mean resid. Stem family"
twoway (scatter mean_residY mean_residX if one==1, mlabel(provin) mlabposition(6) mlabsize(tiny)) ///
 (lfit mean_residY mean_residX if one==1)

 
****************************
****Table A1 Appendix ******
****************************

use ethnoatlas_analysis, clear
 
reg female_agric impartible animal_husbandry econ_level agriculture polit_complex, robust
reg female_agric impartible animal_husbandry econ_level agriculture polit_complex plough, robust


***************************
****Table A2 Appendix******
***************************

use 1887_census, clear

areg inact1887_stan stem_f pop1887 pop1887_2 sex_ratio inactive_male literacy_female dummy_big, abs(ccaa) r
areg inact1887_12_40_stan stem_f pop1887 pop1887_2 sex_ratio inactive_male literacy_female dummy_big, abs(ccaa) r
areg inact1887_41_stan stem_f pop1887 pop1887_2 sex_ratio inactive_male literacy_female dummy_big, abs(ccaa) r


***************************
****Table A3 Appendix******
***************************

use data_main_analysis, clear
collapse stem_f univ secon gdp_pc pop_dens, by(provin year)
merge m:1 provin year using occup_rates, keep(3) nogen

reg occup_female stem_f i.year, r
reg occup_female stem_f i.year pop_dens occup_male , r
reg occup_female stem_f i.year pop_dens occup_male univ secon, r

*****************
****Table 5******
*****************	

use wvs_analysis, clear

reg job_men age female i.education i.marital gdp_pc i.job_status stem_f, r
reg men_politics age female i.education i.marital i.job_status gdp_pc stem_f, 
reg both_contribute age female  i.education i.marital gdp_pc i.job_status stem_f, r
reg job_indep age female i.marital gdp_pc i.job_status stem_f, r /*it does not have education*/


*******************************
****** Table A13 Appendix ******
*******************************

reg satis age female i.marital i.education i.job_status gdp_pc stem_f, r
reg trust age female i.marital i.education i.job_status gdp_pc stem_f, r
reg homo age female i.marital i.education i.job_status gdp_pc stem_f, r
reg eutha age female i.marital i.education i.job_status gdp_pc stem_f, r


* I generate the program that computes the wild-bootstrapped se with cluster at the ccaa level
program def wbootse1
   args depvar o_file
   drop _all
   set seed 0506198120

	*1. Generate file that saves boostrap results
	
	gen b=.
	save bsresults_`o_file', replace
	
	*2. Run the basecase regressions
	
		use wvs_analysis, clear
		
		reg `depvar' age female i.marital i.education i.job_status gdp_pc stem_f, vce(robust)
		gl b_true=_b[stem_f]
		predict epshat, resid
		predict yhat if e(sample), xb
		
	save wvs_bs, replace
	
	*3. Specify the programme that contains the boostrap
	forvalues i = 1(1)1000 {
	use wvs_bs, clear
		bys ccaa: gen temp=uniform()
		bys ccaa: gen pos=(temp[1]<.5)
		gen wildresid=epshat*(2*pos-1)
		gen wildy=yhat+wildresid
		reg wildy age female i.marital i.education i.job_status gdp_pc stem_f, cluster(ccaa)
		gl b=_b[stem_f]	
	clear
	set obs 1
	gen b=$b
	append using bsresults_`o_file'
	save bsresults_`o_file', replace
}
disp `i'
end

wbootse1 job_men jobmen
wbootse1 men_politics menpolitics
wbootse1 both_contribute bothcontribute

wbootse1 satis satisfaction
wbootse1 trust trustinothers
wbootse1 homo homosexuality
wbootse1 eutha euthanasia

***************************************
program def wbootse2
   args depvar o_file
   drop _all
   set seed 0506198120

	*1. Generate file that saves boostrap results
	
	gen b=.
	save bsresults_`o_file', replace
	
	*2. Run the basecase regressions
	
		use wvs_analysis, clear
		
		reg `depvar' age female i.marital i.job_status gdp_pc stem_f, vce(robust)
		gl b_true=_b[stem_f]
		predict epshat, resid
		predict yhat if e(sample), xb
		
	save wvs_bs, replace
	
	*3. Specify the programme that contains the boostrap
	forvalues i = 1(1)1000 {
	use wvs_bs, clear
		bys ccaa: gen temp=uniform()
		bys ccaa: gen pos=(temp[1]<.5)
		gen wildresid=epshat*(2*pos-1)
		gen wildy=yhat+wildresid
		reg wildy age female i.marital i.job_status gdp_pc stem_f, cluster(ccaa)
		gl b=_b[stem_f]	
	clear
	set obs 1
	gen b=$b
	append using bsresults_`o_file'
	save bsresults_`o_file', replace
}
disp `i'
end

wbootse2 job_indep jobindep


